<?php
/**
 * 取消点赞
 * 删除点赞记录 统计当前视频点赞总量
 * update: [lx][2019-01-29]
 */
require_once dirname(dirname(__DIR__)) . "/common.php";

use app\cls\tom\Response;
use app\cls\tom\SysResource;
use app\model\UserZan;
use app\cls\tom\Process;

// 最多开启5个进程
$process = new Process(__FILE__);
$process->setProcessNum(5);
$process->initProcess();

//实例化数据库
$db = SysResource::getDB(true);
$UserZan = new UserZan($db);
$UserZan->setDb($db);
//实例化redis
$redis = SysResource::getRedis();

try {
    $index = 0;
    $runTimes = 200;
    while (true) {
        $index++;
        if ($index > $runTimes) {
            $time = date('Y-m-d H:i:s');
            echo "[{$time}] 干完活了{$runTimes}次，退出!\n";
            break;
        }

        for ($i = 1; $i <= 5; $i++) {
            for ($j = 1; $j <= 100; $j++) {
                $key = REDIS_PREFIX . "fabulous_del_" . $i;
                $res = $redis->rPop($key);
                $data = json_decode($res, true);

                if (!empty($data)) {
                    $time = date('Y-m-d H:i:s');
                    echo "[{$time}]\n[取消点赞]缓存数据：{$res}\n";

                    // 删除点赞流水
                    $res = $db->delete("material_zan", ['my_uuid' => $data['my_uuid'], 'other_uuid' => $data['other_uuid'], 'video_uuid' => $data['video_uuid']]);
                    echo "[取消点赞]删除流水[$res]:" . $db->last_query() . "\n";

                    // 稿件点赞量-1
                    $db->select("id");
                    $db->from("material_zan");
                    $db->where(['other_id' => $data['other_id'], 'video_uuid' => $data['video_uuid']]);
                    $count = $db->count_all_results();
                    $re = $db->update("user_material", ['count_material_love' => $count], ['video_uuid' => $data['video_uuid'], "id" => $data['other_id']]);
                    echo "[取消点赞]修改状态[{$re}]-总数[{$count}]：{$db->last_query()}\n";
                } else {
                    continue;
                }
            }
            sleep(1);
        }
        sleep(1);
    }
} catch (Exception $e) {
    $process->log($e->getMessage());
}